package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/samber/lo"
	"k9-panel/app/adapter/http/internal/convert"
	"k9-panel/app/adapter/internal"
	"k9-panel/app/permission"
	"k9-panel/app/response"
	"k9-panel/app/service"
	"k9-panel/app/service/dto"
	"k9-panel/pkg/gslice"
	"net/http"
)

var (
	HistoryConfig *HistoryConfigController
)

func init() {
	HistoryConfig = new(HistoryConfigController)
}

type HistoryConfigController struct {
	common
}

func (t *HistoryConfigController) Publish(c *gin.Context) {
	var (
		baseAdd internal.ConfigPublishParam
	)
	if err := c.ShouldBind(&baseAdd); err != nil {
		panic(err)
	}

	param := &dto.HistoryConfigPublishParam{
		ProjectId:     baseAdd.ProjectId,
		EnvironmentId: baseAdd.EnvironmentId,
		Content:       baseAdd.Content,
		Memo:          baseAdd.Memo,
	}
	create, err := service.HistoryConfig.Publish(param)
	if err != nil {
		panic(err)
	}
	c.JSON(http.StatusAccepted, response.Success(create))
	return
}

func (t *HistoryConfigController) UnPublish(c *gin.Context) {
	var (
		baseRead internal.ConfigReadParam
	)
	if err := c.ShouldBind(&baseRead); err != nil {
		panic(err)
	}
	configs, err := service.HistoryConfig.UnPublish(uint(baseRead.ProjectId), baseRead.EnvironmentId)
	if err != nil {
		panic(err)
	}
	c.JSON(http.StatusAccepted, response.Success(configs))
	return
}
func (t *HistoryConfigController) Delete(c *gin.Context) {
	var (
		baseRead internal.PrimaryParam
	)
	if err := c.ShouldBind(&baseRead); err != nil {
		panic(err)
	}
	configs, err := service.HistoryConfig.Delete(baseRead.Id)
	if err != nil {
		panic(err)
	}
	c.JSON(http.StatusAccepted, response.Success(configs))
	return
}

func (t *HistoryConfigController) Get(c *gin.Context) {
	var (
		baseAdd internal.PrimaryParam
	)
	if err := c.ShouldBind(&baseAdd); err != nil {
		panic(err)
	}
	create, err := service.HistoryConfig.Get(baseAdd.Id)
	if err != nil {
		panic(err)
	}
	c.JSON(http.StatusAccepted, response.Success(create))
	return
}
func (t *HistoryConfigController) List(c *gin.Context) {
	var (
		baseAdd internal.ListHistoryParam
	)
	if err := c.ShouldBind(&baseAdd); err != nil {
		panic(err)
	}
	create, err := service.HistoryConfig.List(&dto.QueryListHistory{
		ProjectId:     baseAdd.ProjectId,
		EnvironmentId: baseAdd.EnvironmentId,
	})
	if err != nil {
		panic(err)
	}
	rsp := convert.RspListHistory2HistoryRsp(create)

	user, err := t.GetLoginUser(c)
	if err != nil {
		panic(err)
	}
	if !user.Super {
		envIds, err := permission.Perm.GetOwnEnvIDs(c, user.Username, service.Environment)
		if err != nil {
			panic(err)
		}
		rsp.Items = lo.Filter(rsp.Items, func(item *internal.HistoryData, index int) bool {
			return gslice.InSlice(envIds, uint(item.EnvironmentId))
		})
	}
	c.JSON(http.StatusAccepted, response.Success(rsp))
	return
}

func (t *HistoryConfigController) QueryPublish(c *gin.Context) {
	var (
		queryPublishParam internal.QueryPublishParam
	)
	if err := c.ShouldBind(&queryPublishParam); err != nil {
		panic(err)
	}
	create, err := service.HistoryConfig.View(queryPublishParam.EnvName, queryPublishParam.ProjectName)
	if err != nil {
		panic(err)
	}
	if create.File == "" {
		c.JSON(http.StatusInternalServerError, "")
		return
	}
	c.String(http.StatusOK, create.File)
	return
}
